package com.newsanalysis.controller;

import com.newsanalysis.analysis.NewsAnalysisService;
import com.newsanalysis.model.entity.News;
import com.newsanalysis.service.NewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/analysis")
@RequiredArgsConstructor
public class AnalysisController {
    
    private final NewsService newsService;
    private final NewsAnalysisService newsAnalysisService;
    
    @GetMapping
    public String analysisHome(Model model) {
        // 获取新闻统计信息
        int newsCount = (int) newsService.findAll(null).getTotalElements();
        
        // 获取热点新闻
        List<News> hotNews = newsService.findTopHotNews(10);
        
        // 获取各类别新闻数量
        Map<String, Long> categoryDistribution = newsAnalysisService.analyzeCategoryDistribution();
        
        // 获取各情感倾向新闻数量
        Map<String, Long> sentimentDistribution = newsAnalysisService.analyzeSentimentDistribution();
        
        // 设置数据到请求
        model.addAttribute("newsCount", newsCount);
        model.addAttribute("hotNews", hotNews);
        model.addAttribute("categoryDistribution", categoryDistribution);
        model.addAttribute("sentimentDistribution", sentimentDistribution);
        
        return "analysis/home";
    }
    
    @GetMapping("/category")
    public String categoryAnalysis(Model model) {
        // 获取各类别新闻数量
        Map<String, Long> categoryDistribution = newsAnalysisService.analyzeCategoryDistribution();
        
        // 设置数据到请求
        model.addAttribute("categoryDistribution", categoryDistribution);
        
        return "analysis/category";
    }
    
    @GetMapping("/sentiment")
    public String sentimentAnalysis(Model model) {
        // 获取各情感倾向新闻数量
        Map<String, Long> sentimentDistribution = newsAnalysisService.analyzeSentimentDistribution();
        
        // 设置数据到请求
        model.addAttribute("sentimentDistribution", sentimentDistribution);
        
        return "analysis/sentiment";
    }
    
    @GetMapping("/keyword")
    public String keywordAnalysis(@RequestParam(required = false) Long newsId, Model model) {
        if (newsId != null) {
            // 如果指定了新闻ID，则提取该新闻的关键词
            Optional<News> newsOpt = newsService.findById(newsId);
            
            if (newsOpt.isPresent()) {
                News news = newsOpt.get();
                List<Map.Entry<String, Integer>> keywords = newsAnalysisService.extractNewsKeywords(newsId, 20);
                
                // 设置数据到请求
                model.addAttribute("news", news);
                model.addAttribute("keywords", keywords);
                
                return "analysis/news-keyword";
            }
        }
        
        // 否则显示关键词分析首页
        // 获取最新10条新闻
        List<News> newsList = newsService.findAll(null).getContent();
        
        // 设置数据到请求
        model.addAttribute("newsList", newsList);
        
        return "analysis/keyword";
    }
    
    @GetMapping("/hot")
    public String hotAnalysis(Model model) {
        // 获取热点新闻
        List<News> hotNews = newsService.findTopHotNews(20);
        
        // 设置数据到请求
        model.addAttribute("hotNews", hotNews);
        
        return "analysis/hot";
    }

    @GetMapping("/analysis/hot")
    public String showHotAnalysis(Model model) {
        // 获取热门新闻
        List<News> hotNews = newsService.findTopHotNews(10);
//        model.addAttribute("hotNews", hotNews);
//
//        // 获取热度趋势数据
//        List<TrendData> trendData = analysisService.getHotTrend();
//        model.addAttribute("trendData", trendData);
//
//        // 获取热度分布数据
//        List<Integer> distributionData = analysisService.getHotDistribution();
//        model.addAttribute("distributionData", distributionData);

        return "analysis/hot";
    }

    @GetMapping("/analysis/category")
    public String showCategoryAnalysis(@RequestParam(required = false) String category, Model model) {
        // 获取所有分类
//        List<String> categories = newsService.getAllCategories();
//        model.addAttribute("categories", categories);
//
//        // 设置选中的分类
//        String selectedCategory = category != null ? category : categories.get(0);
//        model.addAttribute("selectedCategory", selectedCategory);
//
//        // 获取分类统计数据
//        List<CategoryData> categoryData = analysisService.getCategoryStatistics();
//        model.addAttribute("categoryData", categoryData);
//
//        // 获取分类热度数据
//        List<CategoryHotData> categoryHotData = analysisService.getCategoryHotStatistics();
//        model.addAttribute("categoryHotData", categoryHotData);
//
//        // 获取选中分类的新闻列表
//        List<News> categoryNews = newsService.findByCategory(selectedCategory);
//        model.addAttribute("categoryNews", categoryNews);
        
        return "analysis/category";
    }
}

